//
//  EFengTabController.m
//  WZEfengAndEtong
//
//  Created by wanzhao on 16/1/19.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

#import "EFengTabController.h"
#import "MoreSettingVC.h"
#import "WZNavigationController.h"
#import "ChatBase.h"
#import "ContactVC.h"
#import "CXmppMgr.h"
#import "CMessageMgr.h"
#import "EfengTabView.h"
#import "EFengContactController.h"
#import "EFengAndETongTabBarItem.h"
#import "EFengShareController.h"
#import "CShareCommentMgr.h"
#import "CShareCommentDelMgr.h"
#import "CShareMgr.h"
#import "CShareDelMgr.h"
#import "CUserSettingMgr.h"
#import "CUserSettingModel.h"


@interface EFengTabController ()<UITabBarControllerDelegate, EfengTabViewDelegate>

@property (nonatomic, weak) UITabBarItem *item;
@property (nonatomic, strong) CMessageMgr *messageMgr;
StrongProperty CShareCommentDelMgr *commentDelMgr;
StrongProperty CShareMgr *shareMgr;
StrongProperty CShareCommentMgr *commentMgr;
StrongProperty CShareDelMgr *shareDelMgr;
StrongProperty CUserSettingMgr *settingMgr;
StrongProperty CUserSettingModel *settingModel;

@property (nonatomic, weak) ChatBase *chatVc;
@property (nonatomic, weak) EFengContactController *contactVc;
@property (nonatomic, weak) MoreSettingVC *settingVc;

@property (nonatomic, strong) EfengTabView *tabBarView;
@property (nonatomic, weak) EFengShareController *share;

@end

@implementation EFengTabController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        
    }
    return self;
    
}


- (void)dealloc
{
    NSLog(@"WZTabBarController dealloc");
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:K_XMPPRECEIVEMESSAGE object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"TabBadgeChangeValue" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:K_XMPPUPDATEMESSAGESTATUES object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:K_XMPPUPDATEMESSAGERECORD object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:K_XMPPReceiveComment object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:K_XMPPReceiveShare object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:K_XMPPDelComment object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:K_XMPPDelShare object:nil];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    if (IsIOS7) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    
   
    self.title = @"消息";
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    self.delegate = self;
    self.view.backgroundColor = WhiteColor;
    self.messageMgr = [[CMessageMgr alloc] init];
    self.shareMgr = [[CShareMgr alloc] init];
    self.commentMgr = [[CShareCommentMgr alloc] init];
    self.commentDelMgr = [[CShareCommentDelMgr alloc] init];
    self.shareDelMgr = [[CShareDelMgr alloc] init];
    
    
    self.settingMgr = [[CUserSettingMgr alloc] init];

    self.settingModel = [self.settingMgr getUserSetting];
    self.view.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleMessage) name:K_XMPPRECEIVEMESSAGE object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleMessage) name:K_XMPPUPDATEMESSAGESTATUES object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleMessage) name:K_XMPPUPDATEMESSAGERECORD object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleMessage) name:@"TabBadgeChangeValue" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleShare) name:K_XMPPReceiveShare object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleShare) name:K_XMPPDelShare object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleComment) name:K_XMPPReceiveComment object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleComment) name:K_XMPPDelComment object:nil];
    
    ChatBase *chat = [[ChatBase alloc] init];
    [self addOneController:chat title:@"消息" imageName:@"messageNormalIcon" selectedImageName:@"messageHighLightIcon" tag:0];
    self.chatVc = chat;
    
    EFengContactController *contact = [[EFengContactController alloc] init];
    [self addOneController:contact title:@"联系人" imageName:@"contactNormalIcon" selectedImageName:@"contactHighLightIcon" tag:1];
    self.contactVc = contact;
    
    
    EFengShareController *share = [[EFengShareController alloc] init];
     [self addOneController:share title:@"分享" imageName:@"ico-sharedefault" selectedImageName:[NSString stringWithFormat:@"ico-shareclick"] tag:2];
    self.share = share;

    
    MoreSettingVC *more = [[MoreSettingVC alloc] init];
    [self addOneController:more title:@"我" imageName:@"moreNormalIcon" selectedImageName:[NSString stringWithFormat:@"moreHighLightIcon"] tag:3];
    self.settingVc = more;

    
    [self handleMessage];
    [self handleShare];
    [self handleComment];
    
}
- (EfengTabView *)tabBarView
{
    if (_tabBarView == nil) {
        _tabBarView = [[EfengTabView alloc] init];
        _tabBarView.backgroundColor = WhiteColor;
        _tabBarView.eFengTabDelegate = self;
        _tabBarView.frame = self.tabBar.bounds;
        [self.tabBar addSubview:_tabBarView];
    }
    return _tabBarView;
}
- (void)handleMessage
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
         NSInteger unReadCount = [self.messageMgr loadAllUnReadCount];
        dispatch_async(dispatch_get_main_queue(), ^{
          self.chatVc.badgeItem.badgeValue = [NSString stringWithFormat:@"%zd",unReadCount];
        });
    });
   
    
}

- (void)handleShare
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSInteger unReadCount = 0;
        self.settingModel = [self.settingMgr getUserSetting];
        if ([self.settingModel.yuLiuZiduan1 isEqualToString:@"onlyAttention"]) {
            unReadCount = [self.shareMgr getAttentionShareAllUnReadCount];
        } else {
            unReadCount = [self.shareMgr getShareAllUnReadCount];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            self.share.badgeItem.redDianValue = [NSString stringWithFormat:@"%zd",unReadCount];
        });
    });
}
- (void)handleComment
{
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
        NSInteger unReadCount = [self.commentMgr getShareCommentAllUnReadCount];
        self.share.badgeItem.badgeValue = [NSString stringWithFormat:@"%zd",unReadCount];
    });
}

//必须在以下两个方法中都要删除系统的tabar，否则自定义的tabbar会有黑线条或者重影
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    for (UIView *subView in self.tabBar.subviews) {
        if ([subView isKindOfClass:[UIControl class]]) {
            [subView removeFromSuperview];
        }
    }
}


- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
}
- (void)viewDidAppear:(BOOL)animated
{
    
    [super viewDidAppear:animated];
    
    for (UIView *subView in self.tabBar.subviews) {

        if ([subView isKindOfClass:[UIControl class]]) {
            [subView removeFromSuperview];
        }
    }
}


- (void)addOneController:(EFengAndETongTabBaseController *)viewCotroller title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName tag:(NSInteger)tag
{
       
    EFengAndETongTabBarItem *barItem = [[EFengAndETongTabBarItem alloc] init];
    barItem.selectedImageName = selectedImageName;
    barItem.normalImageName = imageName;
    barItem.badgeValue = @"0";
    barItem.redDianValue = @"0";

    viewCotroller.badgeItem = barItem;
     WZNavigationController *nav = [[WZNavigationController alloc] initWithRootViewController:viewCotroller];
    [self addChildViewController:nav];
    
    
    [self.tabBarView eFengTableViewAddButtonWithItem:barItem];

}


-(void)unSelectedTapTabBarItems:(UITabBarItem *)tabBarItem
{
    [tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                        [UIFont systemFontOfSize:10], UITextAttributeFont,[UIColor whiteColor],UITextAttributeTextColor,
                                        nil] forState:UIControlStateNormal];
}

-(void)selectedTapTabBarItems:(UITabBarItem *)tabBarItem
{
    [tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                        [UIFont systemFontOfSize:10],
                                        UITextAttributeFont,RGBCOLOR(0x00, 0xac, 0xff),UITextAttributeTextColor,
                                        nil] forState:UIControlStateSelected];
}

- (void)eFengTabBarViewSelectControllerFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toindex
{
    self.selectedIndex = toindex;
    if (toindex == 0) {
        self.title = @"消息";
    }else if (toindex == 1){
        self.title = @"联系人";
    }else if (toindex == 2){
        self.title = @"更多";
    } else {
    }

}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
